#!/bin/Rscript

library("ggplot2")

#Risco 1_5_10
#Profits Erro 40%, 20%, -20% e -40%
on_100_1_20 <- read.table("risco_1_5_10/erro_0/on/profits.dat")$V1
op_100_1_20 <- read.table("risco_1_5_10/erro_20/rf/profits.dat")$V1
ut_100_1_20 <- read.table("risco_1_5_10/erro_20/ut/profits.dat")$V1
iosup_100_1_20 <- read.table("risco_1_5_10/erro_20/iosup/profits.dat")$V1
super_100_1_20 <- read.table("risco_1_5_10/erro_20/super/profits.dat")$V1

gop_100_1_20 <- (op_100_1_20 - on_100_1_20) / abs(on_100_1_20)
gut_100_1_20 <- (ut_100_1_20 - on_100_1_20) / abs(on_100_1_20)
giosup_100_1_20 <- (iosup_100_1_20 - on_100_1_20) / abs(on_100_1_20)
gsuper_100_1_20 <- (super_100_1_20 - on_100_1_20) / abs(on_100_1_20)

on_100_1_m20 <- read.table("risco_1_5_10/erro_0/on/profits.dat")$V1
op_100_1_m20 <- read.table("risco_1_5_10/erro_m20/rf/profits.dat")$V1
ut_100_1_m20 <- read.table("risco_1_5_10/erro_m20/ut/profits.dat")$V1
iosup_100_1_m20 <- read.table("risco_1_5_10/erro_m20/iosup/profits.dat")$V1
super_100_1_m20 <- read.table("risco_1_5_10/erro_m20/super/profits.dat")$V1

gop_100_1_m20 <- (op_100_1_m20 - on_100_1_m20) / abs(on_100_1_m20)
gut_100_1_m20 <- (ut_100_1_m20 - on_100_1_m20) / abs(on_100_1_m20)
giosup_100_1_m20 <- (iosup_100_1_m20 - on_100_1_m20) / abs(on_100_1_m20)
gsuper_100_1_m20 <- (super_100_1_m20 - on_100_1_m20) / abs(on_100_1_m20)


on_100_1_40 <- read.table("risco_1_5_10/erro_0/on/profits.dat")$V1
op_100_1_40 <- read.table("risco_1_5_10/erro_40/rf/profits.dat")$V1
ut_100_1_40 <- read.table("risco_1_5_10/erro_40/ut/profits.dat")$V1
iosup_100_1_40 <- read.table("risco_1_5_10/erro_40/iosup/profits.dat")$V1
super_100_1_40 <- read.table("risco_1_5_10/erro_40/super/profits.dat")$V1

gop_100_1_40 <- (op_100_1_40 - on_100_1_40) / abs(on_100_1_40)
gut_100_1_40 <- (ut_100_1_40 - on_100_1_40) / abs(on_100_1_40)
giosup_100_1_40 <- (iosup_100_1_40 - on_100_1_40) / abs(on_100_1_40)
gsuper_100_1_40 <- (super_100_1_40 - on_100_1_40) / abs(on_100_1_40)

on_100_1_m40 <- read.table("risco_1_5_10/erro_0/on/profits.dat")$V1
op_100_1_m40 <- read.table("risco_1_5_10/erro_m40/rf/profits.dat")$V1
ut_100_1_m40 <- read.table("risco_1_5_10/erro_m40/ut/profits.dat")$V1
iosup_100_1_m40 <- read.table("risco_1_5_10/erro_m40/iosup/profits.dat")$V1
super_100_1_m40 <- read.table("risco_1_5_10/erro_m40/super/profits.dat")$V1

gop_100_1_m40 <- (op_100_1_m40 - on_100_1_m40) / abs(on_100_1_m40)
gut_100_1_m40 <- (ut_100_1_m40 - on_100_1_m40) / abs(on_100_1_m40)
giosup_100_1_m40 <- (iosup_100_1_m40 - on_100_1_m40) / abs(on_100_1_m40)
gsuper_100_1_m40 <- (super_100_1_m40 - on_100_1_m40) / abs(on_100_1_m40)



#Risco 5_10_50
#Profits Erro 40%, 20%, -20% e -40%
on_100_5_20 <- read.table("risco_5_10_50/erro_0/on/profits.dat")$V1
op_100_5_20 <- read.table("risco_5_10_50/erro_20/rf/profits.dat")$V1
ut_100_5_20 <- read.table("risco_5_10_50/erro_20/ut/profits.dat")$V1
iosup_100_5_20 <- read.table("risco_5_10_50/erro_20/iosup/profits.dat")$V1
super_100_5_20 <- read.table("risco_5_10_50/erro_20/super/profits.dat")$V1

gop_100_5_20 <- (op_100_5_20 - on_100_5_20) / abs(on_100_5_20)
gut_100_5_20 <- (ut_100_5_20 - on_100_5_20) / abs(on_100_5_20)
giosup_100_5_20 <- (iosup_100_5_20 - on_100_5_20) / abs(on_100_5_20)
gsuper_100_5_20 <- (super_100_5_20 - on_100_5_20) / abs(on_100_5_20)

on_100_5_m20 <- read.table("risco_5_10_50/erro_0/on/profits.dat")$V1
op_100_5_m20 <- read.table("risco_5_10_50/erro_m20/rf/profits.dat")$V1
ut_100_5_m20 <- read.table("risco_5_10_50/erro_m20/ut/profits.dat")$V1
iosup_100_5_m20 <- read.table("risco_5_10_50/erro_m20/iosup/profits.dat")$V1
super_100_5_m20 <- read.table("risco_5_10_50/erro_m20/super/profits.dat")$V1

gop_100_5_m20 <- (op_100_5_m20 - on_100_5_m20) / abs(on_100_5_m20)
gut_100_5_m20 <- (ut_100_5_m20 - on_100_5_m20) / abs(on_100_5_m20)
giosup_100_5_m20 <- (iosup_100_5_m20 - on_100_5_m20) / abs(on_100_5_m20)
gsuper_100_5_m20 <- (super_100_5_m20 - on_100_5_m20) / abs(on_100_5_m20)


on_100_5_40 <- read.table("risco_5_10_50/erro_0/on/profits.dat")$V1
op_100_5_40 <- read.table("risco_5_10_50/erro_40/rf/profits.dat")$V1
ut_100_5_40 <- read.table("risco_5_10_50/erro_40/ut/profits.dat")$V1
iosup_100_5_40 <- read.table("risco_5_10_50/erro_40/iosup/profits.dat")$V1
super_100_5_40 <- read.table("risco_5_10_50/erro_40/super/profits.dat")$V1

gop_100_5_40 <- (op_100_5_40 - on_100_5_40) / abs(on_100_5_40)
gut_100_5_40 <- (ut_100_5_40 - on_100_5_40) / abs(on_100_5_40)
giosup_100_5_40 <- (iosup_100_5_40 - on_100_5_40) / abs(on_100_5_40)
gsuper_100_5_40 <- (super_100_5_40 - on_100_5_40) / abs(on_100_5_40)

on_100_5_m40 <- read.table("risco_5_10_50/erro_0/on/profits.dat")$V1
op_100_5_m40 <- read.table("risco_5_10_50/erro_m40/rf/profits.dat")$V1
ut_100_5_m40 <- read.table("risco_5_10_50/erro_m40/ut/profits.dat")$V1
iosup_100_5_m40 <- read.table("risco_5_10_50/erro_m40/iosup/profits.dat")$V1
super_100_5_m40 <- read.table("risco_5_10_50/erro_m40/super/profits.dat")$V1

gop_100_5_m40 <- (op_100_5_m40 - on_100_5_m40) / abs(on_100_5_m40)
gut_100_5_m40 <- (ut_100_5_m40 - on_100_5_m40) / abs(on_100_5_m40)
giosup_100_5_m40 <- (iosup_100_5_m40 - on_100_5_m40) / abs(on_100_5_m40)
gsuper_100_5_m40 <- (super_100_5_m40 - on_100_5_m40) / abs(on_100_5_m40)

#Erro 0 - Risco 1_5_10
on_100_1_0 <- read.table("risco_1_5_10/erro_0/on/profits.dat")$V1
op_100_1_0 <- read.table("risco_1_5_10/erro_0/rf/profits.dat")$V1
gop_100_1_0 <- (op_100_1_0 - on_100_1_0) / abs(on_100_1_0)

ut_100_1_0 <- read.table("risco_1_5_10/erro_0/ut/profits.dat")$V1
gut_100_1_0 <- (ut_100_1_0 - on_100_1_0) / abs(on_100_1_0)

iosup_100_1_0 <- read.table("risco_1_5_10/erro_0/iosup/profits.dat")$V1
giosup_100_1_0 <- (iosup_100_1_0 - on_100_1_0) / abs(on_100_1_0)

super_100_1_0 <- read.table("risco_1_5_10/erro_0/super/profits.dat")$V1
gsuper_100_1_0 <- (super_100_1_0 - on_100_1_0) / abs(on_100_1_0)

#Erro 0 - Risco 5_10_50
on_100_5_0 <- read.table("risco_5_10_50/erro_0/on/profits.dat")$V1
op_100_5_0 <- read.table("risco_5_10_50/erro_0/rf/profits.dat")$V1
gop_100_5_0 <- (op_100_5_0 - on_100_5_0) / abs(on_100_5_0)

ut_100_5_0 <- read.table("risco_5_10_50/erro_0/ut/profits.dat")$V1
gut_100_5_0 <- (ut_100_5_0 - on_100_5_0) / abs(on_100_5_0)

iosup_100_5_0 <- read.table("risco_5_10_50/erro_0/iosup/profits.dat")$V1
giosup_100_5_0 <- (iosup_100_5_0 - on_100_5_0) / abs(on_100_5_0)

super_100_5_0 <- read.table("risco_5_10_50/erro_0/super/profits.dat")$V1
gsuper_100_5_0 <- (super_100_5_0 - on_100_5_0) / abs(on_100_5_0)



#Gráfico de análise de sensibilidade - Risco 1_5_10
heur <- array(dim=18)
ganh <- array(dim=18)
erro <- array(dim=18)
ci <- array(dim=18)
sh <- array(dim=18)

heur[1] = "UT"
ganh[1] = mean(gut_100_1_20)
erro[1] = 20
ci[1] = 1.96*sd(gut_100_1_20)/sqrt(length(gut_100_1_20))
sh[1] = 19

heur[2] = "UT"
ganh[2] = mean(gut_100_1_m20)
erro[2] = -20
ci[2] = 1.96*sd(gut_100_1_m20)/sqrt(length(gut_100_1_m20))
sh[2] = 19

heur[3] = "UT"
ganh[3] = mean(gut_100_1_0)
erro[3] = 0
ci[3] = 1.96*sd(gut_100_1_0)/sqrt(length(gut_100_1_0))
sh[3] = 19

heur[4] = "UT"
ganh[4] = mean(gut_100_1_40)
erro[4] = 40
ci[4] = 1.96*sd(gut_100_1_40)/sqrt(length(gut_100_1_40))
sh[4] = 19

heur[5] = "UT"
ganh[5] = mean(gut_100_1_m40)
erro[5] = -40
ci[5] = 1.96*sd(gut_100_1_m40)/sqrt(length(gut_100_1_m40))
sh[5] = 19

heur[6] = "RF"
ganh[6] = mean(gop_100_1_20)
erro[6] = 20
ci[6] = 1.96*sd(gop_100_1_20)/sqrt(length(gop_100_1_20))
sh[6] = 3

heur[7] = "RF"
ganh[7] = mean(gop_100_1_m20)
erro[7] = -20
ci[7] = 1.96*sd(gop_100_1_m20)/sqrt(length(gop_100_1_m20))
sh[7] = 3

heur[8] = "RF"
ganh[8] = mean(gop_100_1_0)
erro[8] = 0
ci[8] = 1.96*sd(gop_100_1_0)/sqrt(length(gop_100_1_0))
sh[8] = 3

heur[8] = "RF"
ganh[8] = mean(gop_100_1_40)
erro[8] = 40
ci[8] = 1.96*sd(gop_100_1_40)/sqrt(length(gop_100_1_40))
sh[8] = 3

heur[8] = "RF"
ganh[8] = mean(gop_100_1_m40)
erro[8] = -40
ci[8] = 1.96*sd(gop_100_1_m40)/sqrt(length(gop_100_1_m40))
sh[8] = 3

heur[9] = "COHR"
ganh[9] = mean(giosup_100_1_20)
erro[9] = 20
ci[9] = 1.96*sd(giosup_100_1_20)/sqrt(length(giosup_100_1_20))
sh[9] = 3

heur[10] = "COHR"
ganh[10] = mean(giosup_100_1_m20)
erro[10] = -20
ci[10] = 1.96*sd(giosup_100_1_m20)/sqrt(length(giosup_100_1_m20))
sh[10] = 3

heur[11] = "COHR"
ganh[11] = mean(giosup_100_1_0)
erro[11] = 0
ci[11] = 1.96*sd(giosup_100_1_0)/sqrt(length(giosup_100_1_0))
sh[11] = 3

heur[12] = "COHR"
ganh[12] = mean(giosup_100_1_40)
erro[12] = 40
ci[12] = 1.96*sd(giosup_100_1_40)/sqrt(length(giosup_100_1_40))
sh[12] = 3

heur[13] = "COHR"
ganh[13] = mean(giosup_100_1_m40)
erro[13] = -40
ci[13] = 1.96*sd(giosup_100_1_m40)/sqrt(length(giosup_100_1_m40))
sh[13] = 3

heur[14] = "Super"
ganh[14] = mean(gsuper_100_1_20)
erro[14] = 20
ci[14] = 1.96*sd(gsuper_100_1_20)/sqrt(length(gsuper_100_1_20))
sh[14] = 3

heur[15] = "Super"
ganh[15] = mean(gsuper_100_1_m20)
erro[15] = -20
ci[15] = 1.96*sd(gsuper_100_1_m20)/sqrt(length(gsuper_100_1_m20))
sh[15] = 3

heur[16] = "Super"
ganh[16] = mean(gsuper_100_1_0)
erro[16] = 0
ci[16] = 1.96*sd(gsuper_100_1_0)/sqrt(length(gsuper_100_1_0))
sh[16] = 3

heur[17] = "Super"
ganh[17] = mean(gsuper_100_1_40)
erro[17] = 40
ci[17] = 1.96*sd(gsuper_100_1_40)/sqrt(length(gsuper_100_1_40))
sh[17] = 3

heur[18] = "Super"
ganh[18] = mean(gsuper_100_1_m40)
erro[18] = -40
ci[18] = 1.96*sd(gsuper_100_1_m40)/sqrt(length(gsuper_100_1_m40))
sh[18] = 3

jpeg("sensibilidade_1.jpg")

pd <- position_dodge(.1) # move them .05 to the left and right

lucro <- data.frame(heuristica=heur, ganhos=ganh, err=erro, intervalo=ci, shap=sh)

limits <- aes( x=err, y=100 * ganhos, ymin=100*(ganhos-intervalo), ymax=100*(ganhos+intervalo), colour=heuristica, shape=heuristica)
ggplot(lucro, limits) + 
    scale_y_continuous(breaks=c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)) + scale_x_continuous(breaks=c(-40, -20, -10, 0, 10, 20, 40)) + 
    geom_point(position=pd, size=3)+xlab("Erros de predição") +ylab("Ganho em relação a ON")+opts(title="Análise de sensibilidade em relação ao erro de predição") + theme_bw() + scale_shape_manual(values=c(19, 3)) + geom_errorbar(limits)

dev.off()

#Gráfico de análise de sensibilidade - Risco 5_10_50
heur <- array(dim=18)
ganh <- array(dim=18)
erro <- array(dim=18)
ci <- array(dim=18)
sh <- array(dim=18)

heur[1] = "UT"
ganh[1] = mean(gut_100_5_20)
erro[1] = 20
ci[1] = 1.96*sd(gut_100_5_20)/sqrt(length(gut_100_5_20))
sh[1] = 19

heur[2] = "UT"
ganh[2] = mean(gut_100_5_m20)
erro[2] = -20
ci[2] = 1.96*sd(gut_100_5_m20)/sqrt(length(gut_100_5_m20))
sh[2] = 19

heur[3] = "UT"
ganh[3] = mean(gut_100_5_0)
erro[3] = 0
ci[3] = 1.96*sd(gut_100_5_0)/sqrt(length(gut_100_5_0))
sh[3] = 19

heur[4] = "UT"
ganh[4] = mean(gut_100_5_40)
erro[4] = 40
ci[4] = 1.96*sd(gut_100_5_40)/sqrt(length(gut_100_5_40))
sh[4] = 19

heur[5] = "UT"
ganh[5] = mean(gut_100_5_m40)
erro[5] = -40
ci[5] = 1.96*sd(gut_100_5_m40)/sqrt(length(gut_100_5_m40))
sh[5] = 19

heur[6] = "RF"
ganh[6] = mean(gop_100_5_20)
erro[6] = 20
ci[6] = 1.96*sd(gop_100_5_20)/sqrt(length(gop_100_5_20))
sh[6] = 3

heur[7] = "RF"
ganh[7] = mean(gop_100_5_m20)
erro[7] = -20
ci[7] = 1.96*sd(gop_100_5_m20)/sqrt(length(gop_100_5_m20))
sh[7] = 3

heur[8] = "RF"
ganh[8] = mean(gop_100_5_0)
erro[8] = 0
ci[8] = 1.96*sd(gop_100_5_0)/sqrt(length(gop_100_5_0))
sh[8] = 3

heur[8] = "RF"
ganh[8] = mean(gop_100_5_40)
erro[8] = 40
ci[8] = 1.96*sd(gop_100_5_40)/sqrt(length(gop_100_5_40))
sh[8] = 3

heur[8] = "RF"
ganh[8] = mean(gop_100_5_m40)
erro[8] = -40
ci[8] = 1.96*sd(gop_100_5_m40)/sqrt(length(gop_100_5_m40))
sh[8] = 3

heur[9] = "COHR"
ganh[9] = mean(giosup_100_5_20)
erro[9] = 20
ci[9] = 1.96*sd(giosup_100_5_20)/sqrt(length(giosup_100_5_20))
sh[9] = 3

heur[10] = "COHR"
ganh[10] = mean(giosup_100_5_m20)
erro[10] = -20
ci[10] = 1.96*sd(giosup_100_5_m20)/sqrt(length(giosup_100_5_m20))
sh[10] = 3

heur[11] = "COHR"
ganh[11] = mean(giosup_100_5_0)
erro[11] = 0
ci[11] = 1.96*sd(giosup_100_5_0)/sqrt(length(giosup_100_5_0))
sh[11] = 3

heur[12] = "COHR"
ganh[12] = mean(giosup_100_5_40)
erro[12] = 40
ci[12] = 1.96*sd(giosup_100_5_40)/sqrt(length(giosup_100_5_40))
sh[12] = 3

heur[13] = "COHR"
ganh[13] = mean(giosup_100_5_m40)
erro[13] = -40
ci[13] = 1.96*sd(giosup_100_5_m40)/sqrt(length(giosup_100_5_m40))
sh[13] = 3

heur[14] = "Super"
ganh[14] = mean(gsuper_100_5_20)
erro[14] = 20
ci[14] = 1.96*sd(gsuper_100_5_20)/sqrt(length(gsuper_100_5_20))
sh[14] = 3

heur[15] = "Super"
ganh[15] = mean(gsuper_100_5_m20)
erro[15] = -20
ci[15] = 1.96*sd(gsuper_100_5_m20)/sqrt(length(gsuper_100_5_m20))
sh[15] = 3

heur[16] = "Super"
ganh[16] = mean(gsuper_100_5_0)
erro[16] = 0
ci[16] = 1.96*sd(gsuper_100_5_0)/sqrt(length(gsuper_100_5_0))
sh[16] = 3

heur[17] = "Super"
ganh[17] = mean(gsuper_100_5_40)
erro[17] = 40
ci[17] = 1.96*sd(gsuper_100_5_40)/sqrt(length(gsuper_100_5_40))
sh[17] = 3

heur[18] = "Super"
ganh[18] = mean(gsuper_100_5_m40)
erro[18] = -40
ci[18] = 1.96*sd(gsuper_100_5_m40)/sqrt(length(gsuper_100_5_m40))
sh[18] = 3

jpeg("sensibilidade_5.jpg")

pd <- position_dodge(.1) # move them .05 to the left and right

lucro <- data.frame(heuristica=heur, ganhos=ganh, err=erro, intervalo=ci, shap=sh)

limits <- aes( x=err, y=100 * ganhos, ymin=100*(ganhos-intervalo), ymax=100*(ganhos+intervalo), colour=heuristica, shape=heuristica)
ggplot(lucro, limits) +
    scale_y_continuous(breaks=c(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)) + scale_x_continuous(breaks=c(-40, -20, -10, 0, 10, 20, 40)) +
geom_point(position=pd, size=3)+xlab("Erros de predição") +ylab("Ganho em relação a ON")+opts(title="Análise de sensibilidade em relação ao erro de predição") + theme_bw() + scale_shape_manual(values=c(19, 3)) + geom_errorbar(limits)

dev.off()

#Debug

#paste(">>>> Lucro")
#melhoria_op <- (op_100_1_0) - on_100_1_0
#paste("OP erro 0: ", mean(melhoria_op), "[", mean(melhoria_op) - 1.96*sd(melhoria_op)/sqrt(length(melhoria_op)) , " : ", mean(melhoria_op) + 1.96*sd(melhoria_op)/sqrt(length(melhoria_op)), " ]")

#melhoria_ut <- ut_100_1_0 - on_100_1_0
#paste("UT erro 0: ", mean(melhoria_ut), "[", mean(melhoria_ut) - 1.96*sd(melhoria_ut)/sqrt(length(melhoria_ut)) , " : ", mean(melhoria_ut) + 1.96*sd(melhoria_ut)/sqrt(length(melhoria_ut)), " ]")

#paste(">>>> Ganho")
#paste("OP erro 0: ", mean(gop_100_1_0), "[", mean(gop_100_1_0) - 1.96*sd(gop_100_1_0)/sqrt(length(gop_100_1_0)) , " : ", mean(gop_100_1_0) + 1.96*sd(gop_100_1_0)/sqrt(length(gop_100_1_0)), " ]")

#paste("UT erro 0: ", mean(gut_100_1_0), "[", mean(gut_100_1_0) - 1.96*sd(gut_100_1_0)/sqrt(length(gut_100_1_0)) , " : ", mean(gut_100_1_0) + 1.96*sd(gut_100_1_0)/sqrt(length(gut_100_1_0)), " ]")
